package com.code.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.code.entity.PageResult;
import com.code.entity.Result;
import com.code.pojo.Album;
import com.code.service.AlbumService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("album")
public class AlbumController {

    @Reference
    AlbumService albumService;

    @RequestMapping("findById")
    public Album findById(Integer id) {
        return albumService.findById(id);
    }

    @RequestMapping("findAll")
    public List<Album> findAll() {
        return albumService.findAll();
    }

    @RequestMapping("findPage")
    public PageResult<Album> findPage(Integer page, Integer size, @RequestBody Album album) {
        return albumService.findPage(page, size, album);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result("添加信息成功", 0);
    }

    @RequestMapping("update")
    public Result update(@RequestBody Album album) {
        albumService.update(album);
        return new Result("修改信息成功", 0);
    }

    @RequestMapping("updateImage")
    public Result updateImage(@RequestBody Album album) {
        albumService.updateImage(album);
        return new Result("修改信息成功", 0);
    }

    @RequestMapping("delete")
    public Result delete(Integer id) {
        albumService.delete(id);
        return new Result("删除信息成功", 0);
    }

    @RequestMapping("deleteAll")
    public Result deleteAll(String ids) {
        for (String s : ids.split(",")) {
            albumService.delete(Integer.parseInt(s));
        }
        return new Result("删除信息成功", 0);
    }

    @RequestMapping("delOne")
    public Result delOne(Integer id, String uids) {
        for (String uid : uids.split(",")) {
            albumService.delOne(id, uid);
        }
        return new Result("删除信息成功", 0);
    }

    @RequestMapping("moveAlbum")
    public Result moveAlbum(@RequestBody Map<String, String> moveMap) {
        String albumId = moveMap.get("albumId");
        String toMoveUids = moveMap.get("toMoveUids");
        String movedAlbumId = moveMap.get("movedAlbumId");
        for (String uid : toMoveUids.split(",")) {
            albumService.moveAlbum(Integer.parseInt(albumId), uid, Integer.parseInt(movedAlbumId));
        }
        return new Result();
    }
}


